package com.zhuiyun.educations.api.teacher.service.impl;

import com.zhuiyun.educations.api.rotationimage.entity.ImageFile;
import com.zhuiyun.educations.api.rotationimage.mapper.ImageFileMapper;
import com.zhuiyun.educations.api.teacher.entity.Teacher;
import com.zhuiyun.educations.api.teacher.mapper.TeacherMapper;
import com.zhuiyun.educations.api.teacher.service.TeacherService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.UploadFileUtils;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TeacherServiceImpl
 * @Description TODO 教师业务逻辑层实现类
 * @Author 杨友朋
 * @Date 2023/6/19 15:58
 **/
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherMapper teacherMapper;

    @Resource
    UploadFileUtils uploadFileUtils;

    @Resource
    ImageFileMapper imageFileMapper;

    /**
     * @Author 杨友朋
     * @Description TODO 分页全查教师信息
     * @Date 2023/6/21 20:46
     * @Return PageResult
     * @param teacher，pageRequest
     */
    @Override
    public PageResult selectTeacher(Teacher teacher, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<Teacher> list=teacherMapper.selectTeacher(teacher);
        return MybatisPageHelper.getPageResult(list);
    }

    @Override
    public List<Teacher> selectAll(Teacher teacher) {
        return teacherMapper.selectTeacher(teacher);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加教师信息
     * @Date 2023/6/21 20:46
     * @Return CommonResult
     * @param teacher
     */
    @Override
    @Transactional
    public CommonResult insertTeacher(MultipartFile file, Teacher teacher, ImageFile imageFile) {
        //判断文件是否存在
        if (file!=null){
            //上传至服务器
            String s=uploadFileUtils.uploadFile(file);
            //判断图片id是否为空
            if(!StringUtils.isEmpty(s)){
                imageFile.setImageName(s);
                imageFile.setCreateTime(new Date());
                imageFile.setUpdateTime(new Date());
                imageFile.setFileSize(file.getSize());
                imageFile.setFileUuidName(file.getOriginalFilename());
                imageFile.setFileType(file.getContentType());
                //持久化操作
                imageFileMapper.insertSelective(imageFile);
                //判断实体类id是否为空
                if(teacher.getId()==null){
                    Date date=new Date();
                    teacher.setCreateTime(date);
                    teacher.setUpdateTime(date);
                    teacher.setImageId(Integer.parseInt(s));
                    //持久化操作
                    teacherMapper.insertSelective(teacher);
                    return CommonResult.ok("添加成功");
                }else {
                    return CommonResult.error("添加失败");
                }
            }else {
                return CommonResult.error("文件id为空");
            }
        }else {
            return CommonResult.error("文件为空");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id删除教师信息
     * @Date 2023/6/21 20:46
     * @Return CommonResult
     * @param id
     */
    @Override
    public CommonResult deleteTeacherById(Integer id) {
        // 判断id是否存在 存在则删除
        if(id!=null && id!=0){
            //持久化操作
            teacherMapper.deleteTeacherById(id);
            return CommonResult.ok("删除成功");
        }else {
            return CommonResult.error("删除失败");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据ids批量删除教师信息
     * @Date 2023/6/21 20:47
     * @Return CommonResult
     * @param ids
     */
    @Override
    public CommonResult deleteTeacherByIds(List<Integer> ids) {
        //判断ids是否为空 不为空删除
        if(ids!=null){
            //持久化操作
            teacherMapper.deleteTeacherByIds(ids);
            return CommonResult.ok("批量删除成功");
        }else {
            return CommonResult.error("批量删除失败");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id修改教师信息
     * @Date 2023/6/21 20:46
     * @Return CommonResult
     * @param teacher
     */
    @Override
    public CommonResult updateTeacher(Teacher teacher) {
        //判断教师id是否存在 存在修改
        if(teacher.getId()!=null && teacher.getId()!=0){
            Date date=new Date();
            teacher.setUpdateTime(date);
            //持久化操作
            teacherMapper.updateTeacher(teacher);
            return CommonResult.ok("修改成功");
        }else {
            return CommonResult.error("修改失败");
        }
    }
}
